package org.jivesoftware.openfire.plugin;

import java.io.File;

import org.jivesoftware.openfire.PresenceManager;
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.XMPPServerInfo;
import org.jivesoftware.openfire.container.Plugin;
import org.jivesoftware.openfire.container.PluginManager;
import org.jivesoftware.openfire.interceptor.InterceptorManager;
import org.jivesoftware.openfire.interceptor.PacketInterceptor;
import org.jivesoftware.openfire.interceptor.PacketRejectedException;
import org.jivesoftware.openfire.session.Session;
import org.jivesoftware.openfire.user.UserManager;
import org.jivesoftware.openfire.user.UserNotFoundException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xmpp.packet.JID;
import org.xmpp.packet.Message;
import org.xmpp.packet.Packet;
import org.xmpp.packet.PacketExtension;
import org.xmpp.packet.Presence;

public class OfflineRevert implements PacketInterceptor, Plugin {
	private static final Logger log = LoggerFactory.getLogger(OfflineRevert.class);
	private InterceptorManager interceptorManager;
	private PresenceManager presenceManager;
	private XMPPServer server;
	private XMPPServerInfo serverInfo;
	private UserManager userManager;

	public OfflineRevert() {
		interceptorManager = InterceptorManager.getInstance();
	}

	// Plugin Interface
	public void initializePlugin(PluginManager manager, File pluginDirectory) {
		server = XMPPServer.getInstance();
		interceptorManager.addInterceptor(this);
		userManager = server.getUserManager();
		presenceManager = server.getPresenceManager();
		serverInfo = server.getServerInfo();

		log.info("offlineRevert start success.");
	}

	public void destroyPlugin() {
	}

	@Override
	public void interceptPacket(Packet packet, Session session,boolean incoming, boolean processed) throws PacketRejectedException {
		if (incoming && processed && packet instanceof Message) {
			JID toJID = packet.getTo();
			if (toJID != null) {
				Message msg = (Message) packet;
				if(!msg.getType().equals(Message.Type.chat)){
					return;
				}
				String username = toJID.getNode();
				if (username == null|| !userManager.isRegisteredUser(toJID)) {
					return;
				} else if (!serverInfo.getXMPPDomain().equals(toJID.getDomain())) {
					return;
				}
				Presence status=null;
				try {
					status = presenceManager.getPresence(userManager.getUser(toJID.getNode()));
				} catch (UserNotFoundException e) {
					return;
				}

				if (status == null) {
					JID from = null;
					try {
						for (JID jid : server.getAdmins()) {
							from=jid;break;
						}
					} catch (Exception e) {
						from = null;msg.getTo();
					}
					PacketExtension pe = new PacketExtension("received","urn:xmpp:receipts");  
					pe.getElement().addAttribute("id", msg.getID());
					Message serverRevertMsg = new Message();
					serverRevertMsg.setFrom(from);
					serverRevertMsg.setTo(msg.getFrom());
					serverRevertMsg.addExtension(pe);
					server.getRoutingTable().routePacket(msg.getFrom(), serverRevertMsg, true);
					log.info("offline revert success:"+serverRevertMsg.toXML());
				}
			}
		}
	}
}